package com.spring.hotel.controller;

import com.spring.hotel.common.JsonResponse;
import com.spring.hotel.entity.Orders;
import com.spring.hotel.entity.PageDTO;
import com.spring.hotel.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/hotel/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public JsonResponse insert(@RequestBody Orders orders) {
        ordersService.save(orders);
        return JsonResponse.success(null);
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public JsonResponse updateUser(@RequestBody Orders orders) throws Exception {
        ordersService.updateById(orders);
        return JsonResponse.success(null);
    }

    //删除
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public JsonResponse delete(@PathVariable String id) {
        ordersService.removeById(id);
        return JsonResponse.success(null);
    }

    //分页查询
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResponse list(Orders orders, PageDTO pageDTO) {
        return ordersService.pageList(orders,pageDTO);
    }

    //添加订单
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody Orders orders) throws Exception {
        orders.setId(UUID.randomUUID().toString()); // 设置订单号
        orders.setDate(String.valueOf(LocalDateTime.now()));  //设置时间
        ordersService.save(orders);
        return JsonResponse.success(null);
    }
}
